package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;

public class FeesAdapter extends IBaseAdapter {
	private final int[] ticon = {R.drawable.ico_green,R.drawable.ico_perple,R.drawable.ico_yeloow,R.drawable.ico_orange,R.drawable.ico_blue,R.drawable.ico_greenblue};
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public FeesAdapter(Activity activity, List<JSONObject> itemList) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public TextView name,type;
		public ImageView image;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_faxian_item = R.layout.fees_item;
			convertView = LayoutInflater.from(appContext).inflate(main_faxian_item, null);
			holder.image = (ImageView) convertView.findViewById(R.id.fees_item_icon);
			holder.name = (TextView) convertView.findViewById(R.id.fees_item_name);
			holder.type = (TextView) convertView.findViewById(R.id.fees_item_type);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		JSONObject json = itemList.get(position);
		try {
			holder.name.setText(StringUtil.isEmpty(BaseTools.getFrom("fee", json))?"":"￥"+BaseTools.getFrom("fee", json));
			holder.image.setImageResource(ticon[ticon.length%position]);
			holder.type.setText(BaseTools.getFrom("type", json));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
